package com.primestone.cim.password.generator;

import java.io.UnsupportedEncodingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.primestone.cim.util.HashingUtil;

/**
 * 
 * @author cesar.mateus
 * Jun 25, 2013 11:44:34 AM
 * PasswordGenerator.java
 * CIMPasswordGenerator
 *
 */
public class PasswordGenerator {

	protected Shell shlCimPasswordGenerator;
	protected Text textBase;
	protected Text textHash;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			PasswordGenerator window = new PasswordGenerator();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		this.shlCimPasswordGenerator.open();
		this.shlCimPasswordGenerator.layout();
		while (!this.shlCimPasswordGenerator.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		this.shlCimPasswordGenerator = new Shell();
		this.shlCimPasswordGenerator.setSize(450, 195);
		this.shlCimPasswordGenerator.setText("CIM Password Generator");
		
		Label lblUser = new Label(this.shlCimPasswordGenerator, SWT.NONE);
		lblUser.setBounds(10, 10, 55, 15);
		lblUser.setText("Base: ");
		
		this.textBase = new Text(this.shlCimPasswordGenerator, SWT.BORDER | SWT.PASSWORD);
		this.textBase.setFont(SWTResourceManager.getFont("Arial", 8, SWT.BOLD));
		this.textBase.setBackground(SWTResourceManager.getColor(211, 211, 211));
		this.textBase.setToolTipText("Clear pass");
		this.textBase.setBounds(10, 31, 414, 21);
		
		Button btnGenerate = new Button(this.shlCimPasswordGenerator, SWT.NONE);
		btnGenerate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					PasswordGenerator.this.textHash.setText(HashingUtil.getSHA256AndUTF8(PasswordGenerator.this.textBase.getText()));
				} catch (UnsupportedEncodingException uee) {
					PasswordGenerator.this.textHash.setText("Error: " + uee.getMessage());
					PasswordGenerator.this.textHash.setBackground(SWTResourceManager.getColor(211, 0, 0));
					uee.printStackTrace();
				}
			}
		});
		btnGenerate.setBounds(183, 58, 75, 25);
		btnGenerate.setText("Generate");
		
		this.textHash = new Text(this.shlCimPasswordGenerator, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		this.textHash.setFont(SWTResourceManager.getFont("Arial", 9, SWT.BOLD | SWT.ITALIC));
		this.textHash.setToolTipText("Hash");
		this.textHash.setBounds(10, 90, 414, 47);

	}
}
